// index.js
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    url: 'http://www.tp6.com/index.php', 	//后台请求域名，需要修改你自己的本地或者线上域名
    //url: 'http://www.tp8.com/index.php', 	//后台请求域名，需要修改你自己的本地或者线上域名
    openid: '',
		out_trade_no: '',
		transaction_id: '',
		total_fee: 0.01,
		refund_fee: 0.01,
    motto: 'Hello World',
  },
  wxLogin() {
    wx.login({
      success: (res) => {
        // console.log('登录成功，code为：' + res.code);
        // 获取临时登录凭证code
        wx.request({
          url: this.data.url + '/wechat/getOpenid',
          method: 'POST',
          data: {
            code: res.code
          },
          header: {},
          success: (res) => {
            //console.log(res.data);
            //this.data.openid = res.data.data;
            this.setData({
              openid: res.data.data
            })
          }
        });
      },
      fail: (err) => {
        console.error('登录失败：', err);
      }
    });
  },
  //测试支付
  pay() {
    if (!this.data.openid) {
      wx.showToast({
        title: '请先获取用户openid',
        icon: 'none'
      })
      return false;
    }
    wx.request({
      url: this.data.url + '/wechat/pay',
      method: 'POST',
      data: {
        open_id: this.data.openid
      },
      header: {},
      success: (res) => {
        //console.log(res.data);
        if (res.data.code) {
          const payment = res.data.data;
          wx.requestPayment({
            nonceStr: payment.nonceStr,
            package: payment.package,
            paySign: payment.paySign,
            signType: payment.signType,
            timeStamp: payment.timeStamp,
            success (res) {
              console.log('success:' + JSON.stringify(res));
            },
            fail (err) {
              console.log('fail:' + JSON.stringify(err));
            }
          })
        } else {
          console.log('获取支付参数失败');
        }
      }
    });
  },
  //测试查询订单状态
  checkOrder() {
    if (!this.data.out_trade_no && !this.data.transaction_id) {
      wx.showToast({
        title: '商户订单号和微信订单号不能同时为空！',
        icon: 'none'
      });
      return false;
    }
    wx.request({
      url: this.data.url + '/wechat/checkOrder',
      method: 'POST',
      data: {
        out_trade_no: this.data.out_trade_no,
        transaction_id: this.data.transaction_id
      },
      header: {},
      success: (res) => {
        console.log(res.data);
      }
    });
  },
  //测试退款
  refund() {
    if (!this.data.out_trade_no && !this.data.transaction_id) {
      wx.showToast({
        title: '商户订单号和微信订单号不能同时为空！',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.total_fee) {
      wx.showToast({
        title: '订单金额不能为空！',
        icon: 'none'
      });
      return false;
    }
    if (!this.data.refund_fee) {
      wx.showToast({
        title: '退款金额不能为空！',
        icon: 'none'
      });
      return false;
    }
    wx.request({
      url: this.data.url + '/wechat/refund',
      method: 'POST',
      data: {
        out_trade_no: this.data.out_trade_no,
        transaction_id: this.data.transaction_id,
        total_fee: this.data.total_fee,
        refund_fee: this.data.refund_fee
      },
      header: {},
      success: (res) => {
        console.log(res.data);
        /*if (res.data.code) {
          
        } else {
          console.log('获取支付参数失败');
        }*/
      }
    });
  },
})
